//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.query;

import java.util.*;
import kerjodando.casper.util.StringUtil;

public class FileTupleGroup {

	public ArrayList tuples;

	String hash;

	Long size;

	public FileTupleGroup(String hash, Long size) {
		this.hash = hash;
		this.size = size;
		tuples = new ArrayList();
	}

	public String getHash() {
		return this.hash;
	}

	public Long getSize() {
		return this.size;
	}

	public boolean equals(Object o) {
		if (o instanceof FileTupleGroup) {
			return ((FileTupleGroup) o).getHash().equals(this.getHash())
					&& ((FileTupleGroup) o).getSize().longValue() == this
							.getSize().longValue();
		} else if (o instanceof QueryCompletedFileTuple) {
			return ((QueryCompletedFileTuple) o).getFileHash().equals(
					this.getHash())
					&& ((QueryCompletedFileTuple) o).getSize().longValue() == this
							.getSize().longValue();
		} else if (o instanceof QueryPartialFileTuple) {
			return ((QueryPartialFileTuple) o).getFileHash().equals(
					this.getHash())
					&& ((QueryPartialFileTuple) o).getSize().longValue() == this
							.getSize().longValue();
		} else if (o instanceof QueryRemoteFileTuple) {
			return ((QueryRemoteFileTuple) o).getFileHash().equals(
					this.getHash())
					&& ((QueryRemoteFileTuple) o).getSize().longValue() == this
							.getSize().longValue();
		} else {
			return o == this;
		}
	}

	public long getLastTimeSeen() {
		long lts = 0;
		for (int x = 0; x < this.tuples.size(); x++) {
			if (this.tuples.get(x) instanceof QueryRemoteFileTuple
					&& lts < ((QueryRemoteFileTuple) this.tuples.get(x))
							.getLastTimeSeen().longValue()) {
				lts = ((QueryRemoteFileTuple) this.tuples.get(x))
						.getLastTimeSeen().longValue();
			} else {
				lts = System.currentTimeMillis();
				break;
			}
		}
		return lts;
	}

	public String toString() {
		int owners = this.tuples.size();
		if (this.tuples.size() == 1
				&& this.tuples.get(0) instanceof QueryRemoteFileTuple)
			owners = 0;
		String result = this.getHash() + "  " + ji.JI.i("Owners") + ": "
				+ owners;
		while (result.length() < 50)
			result += " ";
		result += "  " + StringUtil.fileSize2Str(this.getSize().longValue());
		result += " ";
		while (result.length() < 70)
			result += " ";
		result += " " + ((QueryFileTuple) this.tuples.get(0)).getFileName();
		result += ji.JI.i(" [Last seen on") + ": "
				+ new java.util.Date(this.getLastTimeSeen()) + "]    "
				+ ((QueryFileTuple) this.tuples.get(0)).getExtendedInfos();
		return result;
	}

}
